from django.db import models

def getImage(request, image):
     return True
        
class FunctionField(models.Field):
    description = "Returns a function"
    
    __metaclass__ = models.SubfieldBase
    
    def __init__(self, package, *args, **kwargs):
        kwargs['max_length'] = 255
        self.package = package
        super(FunctionField, self).__init__(*args, **kwargs)
    
    def get_prep_value(self, value):
        print "ADDING BACK TO DB"
        if callable(value):
            return value.func_name
        if isinsance(unixode, value):
            print value
            print "UNICODE"
        
    def to_python(self, value):
        print "\n\n\n"
        print "************************"
        print value
        print type(value)
        print callable(value)
        print "************************"
        print "\n\n\n"
        if callable(value):
            print "THIS IS A FUNCTION"
            return value
        x = __import__(self.package, fromlist=self.package)
        try:
            if value != None:
                func = getattr(x, value)
                print value," returned: ",func, type(func)
                return func
        except AttributeError:
            print value, """The function doesn't exist in the module"""
            return False
        except TypeError:
            print """Value isn't a string"""
            return False
        
        